package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhwl.common.enums.vehicle.CardTypeEnum;
import com.xhwl.common.enums.vehicle.InOutEnum;
import com.xhwl.common.enums.vehicle.ReleaseTypeEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.pojo.cent.vehicle.VehcCarRecord;
import com.xhwl.common.pojo.cent.vehicle.VehcPassage;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.common.vo.cent.vehicle.CarRecordVO;
import com.xhwl.common.vo.cent.vehicle.ParkingLotVO;
import com.xhwl.common.vo.cent.vehicle.PassageVO;
import com.xhwl.logistics.dao.VehcCarRecordDao;
import com.xhwl.logistics.dao.VehcPassageDao;
import com.xhwl.logistics.service.IPushDataService;
import com.xhwl.logistics.service.IVehcParkingLotService;
import com.xhwl.logistics.service.IVehcPassageService;
import com.xhwl.logistics.service.cent.IProjectService;

import com.xhwl.logistics.util.DateFormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * @description: 厂商推送
 * @author: lu.Aaron
 * @create: 2021-04-22 10:10
 **/
@Slf4j
//@Service
public class PushDataServiceImpl implements IPushDataService {
    @Autowired
    private VehcCarRecordDao vehcCarRecordDao;
    @Autowired
    private IProjectService iProjectService;
    @Autowired
    private IVehcParkingLotService vehcParkingLotService;
    @Autowired
    private IVehcPassageService vehcPassageService;
    @Autowired
    private VehcPassageDao vehcPassageDao;
    @Override
    public void pushCarRecord(CarRecordVO carRecordVO) {
        log.info("开始进入推送项目车辆进出记录数据接口...");
        log.info("入参pushData：{}", carRecordVO);
        String recordId = carRecordVO.getRecordId();
        Integer status = carRecordVO.getStatus();
        if (StringUtils.isEmpty(recordId)) {
            throw new BusinessException("缺少必要参数：recordId");
        }
        if (null==status) {
            throw new BusinessException("缺少必要参数进出方向：status");
        }
        VehcCarRecord vehcCarRecord = new VehcCarRecord();
        BeanUtils.copyProperties(carRecordVO,vehcCarRecord,"enterTime","existTime","cardType","enterType","existType");
        vehcCarRecord.setEnterTime(DateFormatUtil.stringDate2Date(carRecordVO.getEnterTime()));
        if (carRecordVO.getCardType().equals(CardTypeEnum.FREE.getKey())) {
            vehcCarRecord.setCardType(CardTypeEnum.FREE);
        }else if (carRecordVO.getCardType().equals(CardTypeEnum.MONTH.getKey())){
            vehcCarRecord.setCardType(CardTypeEnum.MONTH);
        }else if (carRecordVO.getCardType().equals(CardTypeEnum.STORE.getKey())){
            vehcCarRecord.setCardType(CardTypeEnum.STORE);
        }else if (carRecordVO.getCardType().equals(CardTypeEnum.TEMPORARILY.getKey())){
            vehcCarRecord.setCardType(CardTypeEnum.TEMPORARILY);
        }
        if (carRecordVO.getEnterType().equals(ReleaseTypeEnum.AUTO.getKey())) {
            vehcCarRecord.setEnterType(ReleaseTypeEnum.AUTO);
        }else if (carRecordVO.getEnterType().equals(ReleaseTypeEnum.MANUAL.getKey())){
            vehcCarRecord.setEnterType(ReleaseTypeEnum.MANUAL);
        }
        if (carRecordVO.getStatus().equals(InOutEnum.IN.getKey())) {
            vehcCarRecord.setStatus(InOutEnum.IN);
        }else if(carRecordVO.getStatus().equals(InOutEnum.OUT.getKey())){
            vehcCarRecord.setStatus(InOutEnum.OUT);
        }
        //通过推送过来的项目code查询项目id,待iot确定方式
        ProjectQuery projectQuery = new ProjectQuery();

        List<Project> projects = iProjectService.list(projectQuery, null);
        if (status.equals(InOutEnum.IN.getKey())) {
            //进场
            vehcCarRecordDao.insert(vehcCarRecord);
        }else if(status.equals(InOutEnum.OUT.getKey())){
            //出场
            if (carRecordVO.getExistType().equals(ReleaseTypeEnum.AUTO.getKey())) {
                vehcCarRecord.setExistType(ReleaseTypeEnum.AUTO);
            }else if (carRecordVO.getExistType().equals(ReleaseTypeEnum.MANUAL.getKey())){
                vehcCarRecord.setExistType(ReleaseTypeEnum.MANUAL);
            }
            vehcCarRecord.setExistTime(DateFormatUtil.stringDate2Date(carRecordVO.getExistTime()));
            vehcCarRecordDao.update(vehcCarRecord,new QueryWrapper<VehcCarRecord>().eq("record_id",recordId));
        }


    }

    @Override
    public void pushParkingLot(ParkingLotVO parkingLotVO)  {
        log.info("开始进入推送车场数据接口...");
        //可能通过供应商传的项目id（或可能需要改成code）查找项目对应的组织projectOrgId enterpriseId level等必要参数（）
        //厂商推送的，接口不再需要同步给厂商 true代表需要 false代表不需要
        parkingLotVO.setIsSynchronize(false);
        vehcParkingLotService.add(parkingLotVO,new Account(),(short) 3);
    }

    @Override
    public void pushPassage(PassageVO passageVO) {
        log.info("开始进入推送通道数据接口...");
        //可能通过供应商传的项目id（或可能需要改成code)查找项目对应的 enterpriseId level等必要参数(现在测试先模拟前端),而areaOrgId（可能通过areaId，和parkingLotId去查）
        //之前捷顺也反映通道和设备是同一概念（这需求厂商能不能实现也是问题 再看吧。。。。）
        //厂商推送的，接口不再需要同步给厂商 true代表需要 false代表不需要
        passageVO.setIsSynchronize(false);
        vehcPassageService.add(passageVO,(short) 3);
    }

    @Override
    public void pushCarInRecord(CarRecordVO carRecordVO) {
        log.info("开始进入推送项目车辆进场记录数据接口...项目：{},唯一记录id:{}",carRecordVO.getProjectName(),carRecordVO.getRecordId());
        VehcCarRecord vehcCarRecord = new VehcCarRecord();
        BeanUtils.copyProperties(carRecordVO,vehcCarRecord,"enterTime","existTime","cardType","enterType","existType");
        vehcCarRecord.setEnterTime(DateFormatUtil.stringDate2Date(carRecordVO.getEnterTime()));
        if (carRecordVO.getCardType().equals(CardTypeEnum.FREE.getKey())) {
            vehcCarRecord.setCardType(CardTypeEnum.FREE);
        }else if (carRecordVO.getCardType().equals(CardTypeEnum.MONTH.getKey())){
            vehcCarRecord.setCardType(CardTypeEnum.MONTH);
        }else if (carRecordVO.getCardType().equals(CardTypeEnum.STORE.getKey())){
            vehcCarRecord.setCardType(CardTypeEnum.STORE);
        }else if (carRecordVO.getCardType().equals(CardTypeEnum.TEMPORARILY.getKey())){
            vehcCarRecord.setCardType(CardTypeEnum.TEMPORARILY);
        }
        if (carRecordVO.getEnterType().equals(ReleaseTypeEnum.AUTO.getKey())) {
            vehcCarRecord.setEnterType(ReleaseTypeEnum.AUTO);
        }else if (carRecordVO.getEnterType().equals(ReleaseTypeEnum.MANUAL.getKey())){
            vehcCarRecord.setEnterType(ReleaseTypeEnum.MANUAL);
        }
        if (carRecordVO.getStatus().equals(InOutEnum.IN.getKey())) {
            vehcCarRecord.setStatus(InOutEnum.IN);
        }else if(carRecordVO.getStatus().equals(InOutEnum.OUT.getKey())){
            vehcCarRecord.setStatus(InOutEnum.OUT);
        }
        //根据厂商推送的enterPassageId查询入场通道id
        String enterPassageId = carRecordVO.getEnterPassageId();
        if (StringUtils.isEmpty(enterPassageId)) {
            throw new BusinessException("入场通道enterPassageId必传");
        }
        VehcPassage vehcPassage = vehcPassageDao.selectOne(new QueryWrapper<VehcPassage>().eq("third_passage_id", enterPassageId));
        vehcCarRecord.setEnterPassageId(vehcPassage.getId());
        vehcCarRecordDao.insert(vehcCarRecord);
        log.info("成功推送项目车辆进场记录数据接口...项目：{},唯一记录id:{}",carRecordVO.getProjectName(),carRecordVO.getRecordId());
    }

    @Override
    public void pushCarOutRecord(CarRecordVO carRecordVO) {
        log.info("开始进入推送项目车辆出场记录数据接口...项目：{},唯一记录id:{}",carRecordVO.getProjectName(),carRecordVO.getRecordId());
        @NotEmpty(message = "记录唯一id必传") String recordId = carRecordVO.getRecordId();
        @NotNull(message = "项目id必传") Integer projectId = carRecordVO.getProjectId();
        log.info("查询是否有进场记录recordId：{}",recordId);
        VehcCarRecord vehcCarRecordFromBD = vehcCarRecordDao.selectOne(new QueryWrapper<VehcCarRecord>().eq("record_id", recordId).eq("project_id", projectId));
        //根据厂商推送的enterPassageId查询入场通道id
        String existPassageId = carRecordVO.getExistPassageId();
        if (StringUtils.isEmpty(existPassageId)) {
            throw new BusinessException("入场通道existPassageId必传");
        }
        VehcPassage vehcPassage = vehcPassageDao.selectOne(new QueryWrapper<VehcPassage>().eq("third_passage_id", existPassageId));

        if (Objects.isNull(vehcCarRecordFromBD)) {
            //代表无入场纪录，直接新增
            VehcCarRecord vehcCarRecord = new VehcCarRecord();
            BeanUtils.copyProperties(carRecordVO,vehcCarRecord,"enterTime","existTime","cardType","enterType","existType");
            vehcCarRecord.setEnterTime(DateFormatUtil.stringDate2Date(carRecordVO.getExistTime()));
            if (carRecordVO.getCardType().equals(CardTypeEnum.FREE.getKey())) {
                vehcCarRecord.setCardType(CardTypeEnum.FREE);
            }else if (carRecordVO.getCardType().equals(CardTypeEnum.MONTH.getKey())){
                vehcCarRecord.setCardType(CardTypeEnum.MONTH);
            }else if (carRecordVO.getCardType().equals(CardTypeEnum.STORE.getKey())){
                vehcCarRecord.setCardType(CardTypeEnum.STORE);
            }else if (carRecordVO.getCardType().equals(CardTypeEnum.TEMPORARILY.getKey())){
                vehcCarRecord.setCardType(CardTypeEnum.TEMPORARILY);
            }
            if (carRecordVO.getExistType().equals(ReleaseTypeEnum.AUTO.getKey())) {
                vehcCarRecord.setExistType(ReleaseTypeEnum.AUTO);
            }else if (carRecordVO.getExistType().equals(ReleaseTypeEnum.MANUAL.getKey())){
                vehcCarRecord.setExistType(ReleaseTypeEnum.MANUAL);
            }
            if (carRecordVO.getStatus().equals(InOutEnum.IN.getKey())) {
                vehcCarRecord.setStatus(InOutEnum.IN);
            }else if(carRecordVO.getStatus().equals(InOutEnum.OUT.getKey())){
                vehcCarRecord.setStatus(InOutEnum.OUT);
            }
            vehcCarRecord.setExistPassageId(vehcPassage.getId());
            vehcCarRecordDao.insert(vehcCarRecord);
        }else {
            //代表有入场记录直接更新
            vehcCarRecordFromBD.setExistTime(DateFormatUtil.stringDate2Date(carRecordVO.getExistTime()));
            if (carRecordVO.getCardType().equals(CardTypeEnum.FREE.getKey())) {
                vehcCarRecordFromBD.setCardType(CardTypeEnum.FREE);
            }else if (carRecordVO.getCardType().equals(CardTypeEnum.MONTH.getKey())){
                vehcCarRecordFromBD.setCardType(CardTypeEnum.MONTH);
            }else if (carRecordVO.getCardType().equals(CardTypeEnum.STORE.getKey())){
                vehcCarRecordFromBD.setCardType(CardTypeEnum.STORE);
            }else if (carRecordVO.getCardType().equals(CardTypeEnum.TEMPORARILY.getKey())){
                vehcCarRecordFromBD.setCardType(CardTypeEnum.TEMPORARILY);
            }
            if (carRecordVO.getExistType().equals(ReleaseTypeEnum.AUTO.getKey())) {
                vehcCarRecordFromBD.setExistType(ReleaseTypeEnum.AUTO);
            }else if (carRecordVO.getExistType().equals(ReleaseTypeEnum.MANUAL.getKey())){
                vehcCarRecordFromBD.setExistType(ReleaseTypeEnum.MANUAL);
            }
            if (carRecordVO.getStatus().equals(InOutEnum.IN.getKey())) {
                vehcCarRecordFromBD.setStatus(InOutEnum.IN);
            }else if(carRecordVO.getStatus().equals(InOutEnum.OUT.getKey())){
                vehcCarRecordFromBD.setStatus(InOutEnum.OUT);
            }
            vehcCarRecordFromBD.setExistPassage(carRecordVO.getExistPassage());
            vehcCarRecordFromBD.setExistPicUrl(carRecordVO.getExistPicUrl());
            vehcCarRecordFromBD.setExistPassageId(vehcPassage.getId());
            vehcCarRecordDao.updateById(vehcCarRecordFromBD);
        }


        log.info("成功推送项目车辆进场记录数据接口...项目：{},唯一记录id:{}",carRecordVO.getProjectName(),carRecordVO.getRecordId());
    }
}

